package team18.yuejian.Activity;

import android.Manifest;
import android.content.Intent;
import android.content.pm.PackageManager;
import android.os.Build;
import android.support.v4.app.ActivityCompat;
import android.support.v4.content.ContextCompat;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import java.util.HashMap;
import java.util.Map;

import okhttp3.MediaType;
import okhttp3.MultipartBody;
import okhttp3.RequestBody;
import team18.yuejian.Model.MineModel;
import team18.yuejian.Model.ShowPicModel;
import team18.yuejian.R;
import team18.yuejian.RetrofitListener;
import team18.yuejian.bean.NoteBean;

public class PicAddActivity extends BaseActivity implements View.OnClickListener, RetrofitListener<NoteBean> {
    private TextView title,intro;
    private ImageView back,addPic;
    private Button add_bt;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_pic_add);
        initView();
        initEvent();
    }

    public void initView(){
        title = findViewById(R.id.AddPic_Title);
        intro = findViewById(R.id.AddPic_intro);
        back = findViewById(R.id.AddPic_back);
        addPic = findViewById(R.id.AddPic_pic);
        add_bt = findViewById(R.id.AddPic_add);
    }

    public void initEvent(){
        back.setOnClickListener(this);
        add_bt.setOnClickListener(this);
        addPic.setOnClickListener(this);
        title.setText("添加晒图");
    }

    @Override
    public void onClick(View view) {
        switch (view.getId()){
            case R.id.AddPic_pic:
                Toast.makeText(PicAddActivity.this,"头像",Toast.LENGTH_SHORT).show();
                if (ContextCompat.checkSelfPermission(PicAddActivity.this,
                        Manifest.permission.WRITE_EXTERNAL_STORAGE)!=PackageManager.PERMISSION_GRANTED){
                    ActivityCompat.requestPermissions(PicAddActivity.this,new String[]{Manifest.permission.WRITE_EXTERNAL_STORAGE},1);
                } else {
                    openAlbum();
                }
                break;

            case R.id.AddPic_add:
                Toast.makeText(PicAddActivity.this,"修改",Toast.LENGTH_SHORT).show();
                /*File file = new File("Mine_Logo.png");*/
                if(PNG_Image.exists()){
                    Toast.makeText(PicAddActivity.this,"文件存在",Toast.LENGTH_SHORT).show();
                }else{
                    Toast.makeText(PicAddActivity.this,"文件不存在",Toast.LENGTH_SHORT).show();
                }
                RequestBody requestBody = RequestBody.create(MediaType.parse("multipart/form-data"),PNG_Image);
                MultipartBody.Part body = MultipartBody.Part.createFormData("sppic", PNG_Image.getName(), requestBody);
                int uid = getUserId();


                RequestBody spContentBody = RequestBody.create(MediaType.parse("multipart/form-data"), intro.getText().toString().trim());
                RequestBody uidBody = RequestBody.create(MediaType.parse("multipart/form-data"), String.valueOf(uid));

                Map<String, RequestBody> map = new HashMap<>();

                map.put("uid",uidBody);
                map.put("spcontent",spContentBody);

                ShowPicModel showPicModel = new ShowPicModel();
                showPicModel.AddShowPic(map,body,this);


                Intent intent = new Intent(PicAddActivity.this,ShowPicActivity.class);
                startActivity(intent);


                break;
        }
    }

    @Override
    public void onSuccess(NoteBean noteBean, int flag) {
        if (noteBean.getStatus().equals("0")){
            Intent intent = new Intent(PicAddActivity.this,ShowPicActivity.class);
            Toast.makeText(PicAddActivity.this,noteBean.getMsg(),Toast.LENGTH_SHORT).show();
            startActivity(intent);
        }else{
            Toast.makeText(PicAddActivity.this,noteBean.getMsg(),Toast.LENGTH_SHORT).show();
        }
    }

    @Override
    public void onFail(String msg) {
            Toast.makeText(PicAddActivity.this,msg,Toast.LENGTH_SHORT).show();
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode,resultCode,data);
        switch (requestCode){
            case CHOOSE_PHOTO:
                if (resultCode == RESULT_OK){
                    if (Build.VERSION.SDK_INT >= 19){
                        handleImageOnKitkat(data,addPic);
                        getImageByView(addPic);
                    } else {
                        handleImageBeforeKitKat(data,addPic);
                    }
                }
                break;

            default: break;
        }
    }
}
